
import { _decorator, Camera, CapsuleCollider, Component, Director, EventKeyboard, instantiate, Label, macro, Node, Prefab, RigidBody, RigidBody2D, SystemEvent, systemEvent, SystemEventType, Vec3 } from 'cc';
import { Ball } from './Ball';
import { Block } from './Block';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {
    @property(Node)
    private ballNode: Node =null!

    @property(Prefab)
    private cubbNode: Prefab =null!


    @property(Label)
    private score:Label =null!



    @property(Camera)
    private mainCamera:Camera=null!

    @property(Camera)
    private ballCamera:Camera=null!

    private cubbArr:Node[]= []

    private gameState=0 //0等待 1开始 2结束

    private initScore=0

    //当前场景的最后一块跳板
    private lastNode:Node=null!

    protected onLoad(): void {
        systemEvent.on(Node.EventType.TOUCH_START, this.move, this)
        systemEvent.on(SystemEventType.KEY_DOWN,this.onKeyDown,this)
        this.initBlock()
    }

    protected update(dt: number): void {
        if(this.gameState==1){
            if(this.ballNode.getPosition().y<-4){
                console.log("游戏结束");
                this.gameState=2
                Director.instance.loadScene("game")
                return
                
            }
            let speed=-2*dt

            for (let blockNode of this.cubbArr ) {
                let nowNode=blockNode.getPosition()
                let nextNodeX= nowNode.x += speed

                //飞出屏幕外面的板块
                if(nextNodeX<=-2){
                    this.addScort(1)
                    blockNode.setPosition(this.lastNode.getPosition().x+2,nowNode.y,nowNode.z)
                    this.lastNode=blockNode
                    
                    let blockComponent=blockNode.getComponent(Block)
                    blockComponent.init(0.5+0.6*Math.random(),(Math.random()>0.5?1:-1)*((Math.random())*0.5))


                }else{
                    blockNode.setPosition(nextNodeX,nowNode.y,nowNode.z)
                }
                
            }
            
        }
    }

    //键盘回调
    onKeyDown(event:EventKeyboard){
        switch(event.keyCode){
            case macro.KEY.space:
                this.switchCamera()
                break;
        }

    }

    switchCamera(){
        this.mainCamera.node.active=!this.mainCamera.node.active
        this.ballCamera.node.active=!this.ballCamera.node.active
    }
    move() {
        let ball=this.ballNode.getComponent(Ball)
        ball.Ballmove()

        if(this.gameState==0){
            this.gameState=1
        }

    }


    initBlock(){
        for (let i = 0; i < 5; i++) {
            let cubb=instantiate(this.cubbNode)
            this.node.addChild(cubb)
            this.cubbArr.push(cubb)
            cubb.setPosition(new Vec3(i*2,0,0))
            
        }

        this.lastNode=this.cubbArr[this.cubbArr.length-1]
    }


    addScort(value:number){
        this.initScore+=value;
        this.score.string=String(this.initScore)
    }
}

